$(document).ready(function () {
    // 表单校验规则
    $("#registrationForm").validate({
        rules: {
            username: "required",
            password: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            username: "请输入用户名",
            password: "请输入密码",
            email: "请输入有效的邮箱地址"
        },
        submitHandler: function (form) {
            // 使用ajax提交表单数据
            var formData = $("#registrationForm").serializeArray(); // 序列化表单数据
            var jsonData = {};

            // 将表单数据转换为JSON对象
            $.each(formData, function (index, item) {
                jsonData[item.name] = item.value;
            });

            $.ajax({
                url: "/api/user/register",
                method: "POST",
                dataType: 'json',
                data: JSON.stringify(jsonData),
                contentType: 'application/json;charset=utf-8',
                success: function (req) {
                    form.reset();
                    console.log(req)
                    window.location.href = "/list.html"
                },
                error:function(e){
                    console.log('error')
                    console.log(e)
                    window.location.href = "/list.html"
                }
            });
        }
    });
});
